package com.daatak.finance.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daatak.finance.calculators.CalculatorI;
import com.daatak.finance.calculators.CalculatorObjectI;
import com.daatak.finance.calculators.CalculatorUtil;
import com.daatak.finance.dao.FinancialsDaoI;
import com.daatak.finance.domain.FinancialProductObject;
import com.daatak.finance.domain.RateBandObject;
import com.daatak.finance.domain.RateCardObject;

@Service
public class FinancialBean implements FinancialBeanI {
	
	@Autowired
	FinancialsDaoI financialDao;
	
	@Autowired
	private CalculatorUtil calcUtil;
	
	
	public List<FinancialProductObject> list() {
		// TODO Auto-generated method stub
		return financialDao.list();
	}

	public List<RateBandObject> rateBandsByRateCardId(Integer rateCardId) {
		return financialDao.getRateBandByRateCardId(rateCardId);
	}

	public List<RateBandObject> listRateBand(Integer rateCardId,
			Integer financialProductId) {
		// TODO Auto-generated method stub
		return financialDao.getRateBandByRateCardId(rateCardId);
	}

	public FinancialProductObject findById(Integer id) {
		// TODO Auto-generated method stub
		return financialDao.findById(id);
		
	}

	public RateCardObject rateCardById(Integer rateCardId) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<RateCardObject> listRateCards(Integer financialProductId) {
		// TODO Auto-generated method stub
		return financialDao.listRateCards(financialProductId);
	}

	public void setFinancialDao(FinancialsDaoI financialDao) {
		this.financialDao = financialDao;
	}

	@Override
	public String createFinancialProduct(FinancialProductObject financialProduct) {
		financialDao.createFinancialProduct(financialProduct);
		return "SUCCESS";
	}

	@Override
	public void addRateCard(RateCardObject rateCardObject) {
		financialDao.createRateCard(rateCardObject);
		
	}

	@Override
	public void create(RateBandObject rateBandObject) {
		financialDao.create(rateBandObject);
	}

	@Override
	public void deleteRate(Integer id) {
		// TODO Auto-generated method stub
		financialDao.deleteRate(id);
	}

	@Override
	public void deleteRateBand(Integer id) {
		// TODO Auto-generated method stub
		financialDao.deleteRateBand(id);
		
	}

	@Override
	public void initiatCalculator(CalculatorObjectI calculatorObject) {
		
	
		List<RateBandObject> rates = financialDao.getRateBands(calculatorObject.getReferenceId(), calculatorObject.getFinancedAmount(),calculatorObject.getTerm());
		if(rates.size()>0){
			calculatorObject.setRate(rates.get(0).getRate());
		}
		else
		{
			calculatorObject.setStatus("ERROR");
			calculatorObject.setDescription("No rate band found.");
		}
		
	}

	@Override
	public void doCalculation(CalculatorObjectI calculatorObject) {
		// TODO Auto-generated method stub
		initiatCalculator(calculatorObject);
		
		CalculatorI calculator = calcUtil.getCalculator(calculatorObject);
		calculator.calculate();
		//calculator.addExtraAmount(findById(calculatorObject.getReferenceId()).getDealerFees());
		
	}
	
	
	


}
